﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Adv;

namespace CMS.Controls.Adv
{
    public partial class SlideShow : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ListAdvByTypeId();
        }

        private void ListAdvByTypeId()
        {
            lvList.DataSource = AdvController.Adv_GetAdvSlideShow();
            lvList.DataBind();
        }

        protected void lvList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (string.Equals(e.CommandName, "DeleteAdv"))
            {
                var dataItem = (ListViewDataItem)e.Item;
                var id = ConvertUtility.ToInt32(lvList.DataKeys[dataItem.DisplayIndex].Value);

                if (id > 0)
                {
                    var o = AdvController.Adv_GetById(id);
                    if (o != null)
                    {
                        AdvController.Adv_Delete(o.AdvId);
                        ltMessage.Text = MessageUtility.ShowMessage(null, "Xóa quảng cáo thành công.", MessageType.Information);
                    }
                }

                ListAdvByTypeId();
            }
        }

        protected void BtnSave(object sender, EventArgs e)
        {
            var lstAdvImage = txtImageList.Text;

            if (!string.IsNullOrEmpty(lstAdvImage))
            {
                var splitAdv = lstAdvImage.Split('►');

                if (splitAdv.Length > 0)
                {                    
                    for (int i = 0; i < splitAdv.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(splitAdv[i]))
                        {
                            var o = new AdvObject();
                            o.TypeId = (int)AdvType.SlideShow;
                            o.Priority = i + 1;
                            o.ImagePath = splitAdv[i];
                            o.Url = string.Empty;
                            AdvController.Adv_Create(o);
                        }
                    }

                    ListAdvByTypeId();

                    ltMessage.Text = MessageUtility.ShowMessage(null, "Cập nhật thành công.", MessageType.Information);
                }
            }
        }
    }
}